# Copyright (c) 2018 Presto Labs Pte. Ltd.
# Author: leon

from coin.exchange.base.kr_rest.native_public_client_base import NativePublicClientBase


class BittrexNativePublicClient(NativePublicClientBase):
  BASE_URL_V1_1 = 'https://bittrex.com/api/v1.1/public'

  def __init__(self):
    NativePublicClientBase.__init__(self, url=self.BASE_URL_V1_1, timeout=3)

  def query_getticker(self, native_symbol):
    path = 'getticker?market=%s' % native_symbol
    return self._get_as_json(path)

  def query_getorderbook(self, native_symbol):
    path = 'getorderbook?market=%s&type=both' % native_symbol
    return self._get_as_json(path)

  def query_getmarketsummaries(self):
    path = 'getmarketsummaries'
    return self._get_as_json(path)

  def query_detailed_ticker(self, native_symbol):
    result = self.query_getmarketsummaries()
    if result.status is None:
      for summary in result.msg['result']:
        if summary['MarketName'] == native_symbol:
          result.msg = summary
          break
    return result

  def queryAll(self):
    print(self.query_getticker('BTC-ETH'))
    print(self.query_getorderbook('BTC-ETH'))

  def query_getmarkets(self):
    path = 'getmarkets'
    return self._get_as_json(path)

  def query_MinTradeSize(self):
    markets = self.query_getmarkets()
    min_trade_sizes = {}
    for market in markets.msg['result']:
      native_symbol = '%s-%s' % (market['BaseCurrency'], market['MarketCurrency'])
      min_trade_sizes[native_symbol] = market['MinTradeSize']
    return min_trade_sizes
